import axios from "axios";
import base from './config';
// 模拟 API 基础 URL
// const baseURL = "http://localhost:8080";
const baseURL= `${base}`

// 获取分类数据
export const fetchCategories = async () => {
  try {
    const response = await axios.get(`${baseURL}/category/list`);
    return response.data;
  } catch (error) {
    console.error("获取分类数据失败:", error);
    throw error;
  }
};

// 获取种子列表
export const fetchSeedList = async (slug, page) => {
  try {
    const response = await axios.get(`${baseURL}/torrent/${slug}/list`, {
      params: {
        page,
      },
    });
    return response.data;
  } catch (error) {
    console.error("获取种子列表失败:", error);
    throw error;
  }
};



// 上传种子
// test
export const uploadSeed = async (formData) => {
  try {
    console.log(formData);
    const response = await axios.post(`${baseURL}/torrent/upload`, formData, {
      headers: {
        "Content-Type": "multipart/form-data", // 必须设置为 multipart/form-data
      },
    });
    return response.data; // 返回后端的响应数据
  } catch (error) {
    console.error("上传种子失败:", error);
    throw error;
  }
};
//test
// 上传图片
export const uploadImage = async (formData) => {
  return axios({
    url: `${baseURL}/file/uploadImage`,
    method: "post",
    data: formData,
    headers: {
      "Content-Type": "multipart/form-data",
    },
  });
};

// 获取种子详情
//test
export const fetchSeedDetail = async (id) => {
  try {
    const response = await axios.get(`${baseURL}/torrent/${id}`);
    console.log(response.data);
    
    return response.data.data; // 返回种子详情数据
  } catch (error) {
    console.error("获取种子详情失败:", error);
    throw error;
  }
  //   return new Promise((resolve) => {
  //     setTimeout(() => {
  //       resolve({
  //         id,
  //         title: '塞尔达传说',
  //         description: `# 游戏简介
  // 这是一款经典的开放世界冒险游戏。

  // ## 游戏特色
  // - 开放世界探索
  // - 丰富的解密要素
  // - 独特的战斗系统

  // ![游戏截图](http://example.com/zelda.jpg)

  // ## 配置要求
  // - 系统：Windows 10 64位
  // - 处理器：Intel i5-6600k或更高
  // - 内存：8GB RAM`,
  //         downloadLink: 'http://localhost:8080/download/seed1.torrent',
  //         author: 'alice',
  //         category: '游戏',
  //         tags: [
  //           { label: '动作冒险', value: 'action' },
  //           { label: '开放世界', value: 'openworld' }
  //         ],
  //         liked: true,
  //         collected: true,
  //         likes: 300,
  //         downloads: 1200,
  //         comments: [
  //           { username: 'Alice', content: '这是一个很棒的种子！', replyTo: null },
  //           { username: 'Bob', content: '感谢分享！', replyTo: null },
  //           { username: 'Charlie', content: '期待更多类似的资源！', replyTo: 'Alice' },
  //         ]
  //       });
  //     }, 1000);
  //   });
};

// 新增获取评论的函数
export const fetchComments = async (id) => {
  try {
    const response = await axios.get(`${baseURL}/torrent/${id}/comment/list`);
    return response.data.data; // 返回评论数据
  } catch (error) {
    console.error("获取评论失败:", error);
    throw error;
  }
};

// 点赞种子
export const likeSeed = async (id, userId) => {
  try {
    const response = await axios.post(
      `${baseURL}/torrent/${id}/like`,
      {},
      {
        params: {
          userId,
        },
      }
    );
    return response.data; // 返回点赞后的状态
  } catch (error) {
    console.error("点赞失败:", error);
    throw error;
  }
};
// 取消点赞
export const unlikeSeed = async (id, userId) => {
  try {
    const response = await axios.post(
      `${baseURL}/torrent/${id}/unlike`,
      {},
      {
        params: {
          userId,
        },
      }
    );
    return response.data; // 返回点赞后的状态
  } catch (error) {
    console.error("点赞失败:", error);
    throw error;
  }
};

// 收藏种子
export const collectSeed = async (id, userId) => {
  try {
    const response = await axios.post(
      `${baseURL}/torrent/${id}/favorite`,
      {},
      {
        params: {
          userId,
        },
      }
    );
    return response.data; // 返回收藏后的状态
  } catch (error) {
    console.error("收藏失败:", error);
    throw error;
  }
};

// 取消收藏
export const uncollectSeed = async (id, userId) => {
  try {
    const response = await axios.post(
      `${baseURL}/torrent/${id}/unfavorite`,
      {},
      {
        params: {
          userId,
        },
      }
    );
    return response.data; // 返回收藏后的状态
  } catch (error) {
    console.error("收藏失败:", error);
    throw error;
  }
};

// 发表评论
export const postComment = async (torrentId, userId, comment, replyName = null) => {
  // 模拟发表评论的 API 调用
  // 实际开发中应替换为真实的 API 调用
  const response = await axios.post(`${baseURL}/torrent/${torrentId}/comment/add`, {
    torrentId,
    userId, // 用户名
    content: comment, // 评论内容
    replyName, // 回复对象（如果有）
  });
  return response.data; // 返回评论后的状态

  // return new Promise((resolve) => {
  //   setTimeout(() => {
  //     resolve({
  //       comment: {
  //         username,
  //         content: comment,
  //         replyTo,
  //       },
  //       message: "评论成功！",
  //     });
  //   }, 500);
  // });
};

// 获取筛选策略的函数
export const fetchPromotionStrategies = async () => {
  try {
    const response = await axios.get(`${baseURL}/torrent/promotionPolicy`); // 请替换为实际接口地址
    return response.data;
  } catch (error) {
    console.error('获取筛选策略失败:', error);
    throw error;
  }
};

// 获取表情
export const fetchEmojis = async () => {
  try {
    const response = await axios.get(`${baseURL}/emoji/list`);
    return response.data;
  } catch (error) {
    throw error;
  }
};

// 获取 tracker URL
export const getTrackerUrl = async () => {
  // 模拟获取 tracker URL 的 API 调用
  // const response = await axios.get(`${baseURL}/tracker/url`);
  // return response.data.trackerUrl; // 返回 tracker URL
  // 实际开发时替换为真实的 API 调用
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        trackerUrl: "http://47.113.111.115:8080/announce",
      });
    }, 200);
  });
};

//删除评论
export const deleteComment = async (seedId, commentId) => {
  try {
    const response = await axios.post(`${baseURL}/torrent/${seedId}/comment/delete`, null, {
      params: {
        commentId: Number(commentId), // 作为查询参数传递
      },
    });
    return response.data;
  } catch (error) {
    console.error("删除评论失败:", error);
    throw error;
  }
};
